#include<stdio.h>
#include<conio.h>
#include<math.h>

#define size 50

/*
Owais Lone
Roll no: 25133
Class: SE-II

Polynomial Operations
*/


//Function Prototypes
int input(int *a,char value[]);
int add(int *a,int *b,int *c,int m,int n);
int eval(int *a,int x,int l);
void display(int *a,int l);
void main()//Main Program
{
int i,a[size],b[size],c[size],*aa,*bb,m,*cc,n,l,r,x;
char ch,run;
clrscr();
for(i=0;i<=size;i++)
{
a[i]=0;
b[i]=0;
c[i]=0;
}
aa=&a[0];
bb=&b[0];
cc=&c[0];
run='y';
do
{
flushall();
printf("\nPolynomial Operations\n\n(A)ddition\t\t\t(S)ubtraction\n(M)ultiplication\t\t(E)valuate\n\t\t   (Q)uit\n\nchoice:>");
ch=getche();
   switch(ch)
   {
	case 'a':
	case 'A'://Addition
		clrscr();
		m=input(aa,"First");
		n=input(bb,"Second");
		l=add(aa,bb,cc,m,n);
		display(cc,l);

	break;

	case 's':
	case 'S'://Subtraction
		clrscr();
		m=input(aa,"First");
		n=input(bb,"Second");
		l=sub(aa,bb,cc,m,n);
		display(cc,l);

	break;

	case 'm':
	case 'M'://Multiplication
		clrscr();
		m=input(aa,"First");
		n=input(bb,"Second");
		l=mul(aa,bb,cc,m,n);


		break;

	case 'e':
	case 'E':

	printf("Enter the value of X\nX=");
	scanf("%d",&x);
	clrscr();
	m=input(aa,"");
	r=eval(aa,x,m);
	clrscr();
	printf("Value of ");
	display(aa,m);
	printf(" for X=%d is %d",x,r);




	break;

	case 'q':
	case 'Q':
	run='n';
   }//End Switch
   getch();
   clrscr();

}while(run=='y'||run=='Y');
}

int input(int *a,char value[])//Input Polynomial
{
int i,h;
printf("Enter the highest power for %s polynomial\nPower=",value);
scanf("%d",&h);
clrscr();
	for(i=h;i>=0;i--)
	{
	printf("Coeff for x^%d\nCoeff=",i);
	scanf("%d%",&a[i]);
	clrscr();
	}
	return(h);
}

int add(int *a,int *b,int *c,int m,int n)//Addition
{
int i=0,l;
if(m>n)
l=m;
else
l=n;
	for(i=0;i<=l;i++)
	{
	c[i]=a[i]+b[i];
	}
	i--;
return i;
}


int sub(int *a,int *b,int *c,int m,int n)//Addition
{
int i=0,l;
if(m>n)
l=m;
else
l=n;
	for(i=0;i<=l;i++)
	{
	c[i]=a[i]-b[i];
	}
	i--;
return i;
}

int eval(int *a,int x,int m)
{
int i,r=0;
  for(i=0;i<=m;i++)
  {
  r=(a[i]*(pow(x,i)))+r;
  }
return r;


}


int mul(int *a,int *b,int *c,int m,int n)//Addition
{
int s,i=0,l,j,k;
if(m>n)
l=m;
else
l=n;
s=m+n;
	for(i=l;i>=0;i--)
	{
	   for(j=l;j>=0;j--)
	   {
	     k=i+j;
	     c[k]=a[i]*b[j];

	   }
	}
	for(k=s;k>=0;k--)
	{
	  printf("%dx^%d + ",c[k],k);
	}
	printf("\b\b\b   ");
return(k);
}


void display(int *a,int l)
{
int i;
	for(i=l;i>=0;i--)
	{

	printf("%dx^%d + ",a[i],i);
	}
	printf("\b\b\b   ");
}